package com.example.broadcast;

import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.broadcast.receiver.AlarmReceiver;

public class PendingAlarmAcivity extends AppCompatActivity implements View.OnClickListener {

    private AlarmReceiver receiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_standard_broadcast);
        findViewById(R.id.btn_broadcast).setOnClickListener(this);
        Log.d("tang", "onCreate: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        receiver = new AlarmReceiver(getApplicationContext());
        IntentFilter filter = new IntentFilter(AlarmReceiver.ALARM_ACTION);
        registerReceiver(receiver, filter);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d("tang", "onConfigurationChanged: ");
        switch (newConfig.orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                Log.d("tang", "onConfigurationChanged: 屏幕为竖直方向");
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                Log.d("tang", "onConfigurationChanged: 屏幕为水平方向");
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(receiver);
    }

    @Override
    public void onClick(View v) {
        receiver.sendAlarm();//相当于开始发送广播（sendBroadcast）
    }
}
